﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Admin.Core;
using ZhonTai.Admin.Core.SignalR;
using ZhonTai.Industry.Contracts.Communication;

namespace ZhonTai.Industry.Contracts.Interactive
{
    public class StateMonitor : BaseMonitor
    {
        private SiemensPlc _plc;
        private string _addr;
        private readonly IHubContext<MessageHub> _hub;
        private string _opCode;

        public StateMonitor(int initialInterval = 1000) : base(initialInterval)
        {
            _hub = AppInfo.GetRequiredService<IHubContext<MessageHub>>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="addr"></param>
        /// <param name="opCode"></param>
        public void Initialization(SiemensPlc plc, string addr, string opCode)
        {
            _plc = plc;
            _addr = addr;
            _opCode = opCode;
        }
        protected override void Execute()
        {
            var result = _plc.ReadInt16(_addr);
            if (result.IsSuccess)
            {
                _hub.Clients.All.SendAsync(_opCode + "State", result.Content);
            }
        }
    }
}